 <?php 

  require "ConnDB.php";
  require "Model.php";
  class UserModel extends Model{
 	

    public function __construct(){
       parent::__construct();
    }

    public function getUserList(){
      // $data=array(array("张三","20","登山"),array("李四","20","登山游泳"));
      $sql="select * from users";
      $data=$this->conn->query($sql);// $conn 不能直接调用query
      return $data; 
 	
    }
    public function addUserInfo(){
      // 把用户的信息写在数据库
   // 上传头像
    $pic=$this->uploadfile("pic",'images/');


    $username=trim($_POST['username']);
    $password=$_POST['password'];
    $gender=$_POST['gender'];
    $birthdate=$_POST['birthdate'];
    $hobby=implode($_POST['hobby'],', ');//将数组转换成字符串
    $degree=$_POST['degree'];
    $intro=$_POST['intro'];

   // var_dump($_POST);
    $sql="insert into users values(null,'$username',md5('$password'),'$gender','$birthdate','$hobby','$degree','$intro','$pic')";
    
    $result=$this->conn->query($sql);//写入数据库
    return $result;

   

  }
  
  // 更新用户，获取用户信息
  // public function getUserbyID(){
  //   $uid=$_GET['id'];
  //   $sql="selct * from users where uid=$uid";
  //   $data=$this->conn->query($sql);
  //   return $data;
  // }
  public function getUserbyID($id){
      $sql = "select * from users where uid=".$id;
        $result = $this->conn->query($sql);
          return $result;
    }


  public function updateUserInfo(){
     
     $pic=$this->uploadfile("pic",'images/');
    
    $id=$_POST['id'];
    $uname=trim($_POST['uname']);
    $birthdate=$_POST['birthdate'];
    $gender=$_POST['gender'];
    $hobby=implode($_POST['hobby'],',');
    $degree=$_POST['degree'];
    $intro=$_POST['intro'];

    $sql="update users set uname='{$uname}',birthdate='{$birthdate}',gender='{$gender}',hobby='{$hobby}',degree='{$degree}',intro='{$intro}',pic='{$pic}' where uid={$id}";
  // echo $sql;
   $result=$this->conn->query($sql);
   return $result;
    }

  public function delUser($id){
      $sql = "delete from users where uid={$id}";
      $result = $this->conn->query($sql);
      return $result;
    }






    }


 ?>